import store from './../store';
import {
  CLEAR_TOKEN,
  SET_TOKEN,
} from "./../store/mutation-types.js";

//获取url参数
export const getUrlParame = name => {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};
/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
};

//获取token
export const getToken = ()=>{
  const token = store.state.token;
  if(token) {
    return token;
  }else{
    const t = getStore('token');
     store.commit(SET_TOKEN,t);
     return t;
  }
}
// 退出登录
export const loginOut= ()=>{
  store.commit(CLEAR_TOKEN)
}

//获取user_info
export const getUserInfo = ()=>{

}

//获取wx_info
export const getWeiXinInfo = () =>{

}

// 腾讯地图定位
export const getTXLocation = () =>{
  let geolocation = new qq.maps.Geolocation();
  return new Promise(function(resolve, reject){
    let showPosition = function(position) {
      let adCode = position.adCode; //邮政编码
      let nation = position.nation; //中国
      let city = position.city; //城市
      let addr = position.addr; //详细地址
      let province = position.province;
      let lat = position.lat; //
      let lng = position.lng; //火星坐标 //TODO 实现业务代码逻辑
      console.log(position);
      // alert(JSON.stringify(position));
      resolve(position);
    };
    let options = { timeout: 15000 };
    let showErr = function(err) {
      console.log("出错了",JSON.stringify(err));
      reject(err);
      // alert('出错了');
      // alert(JSON.stringify(err));
    };
    geolocation.getLocation(showPosition, showErr, options);
  });
  
}



/**
 * 获取style样式
 */
export const getStyle = (element, attr, NumberMode = "int") => {
  let target;
  // scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
  if (attr === "scrollTop") {
    target = element.scrollTop;
  } else if (element.currentStyle) {
    target = element.currentStyle[attr];
  } else {
    target = document.defaultView.getComputedStyle(element, null)[attr];
  }
  //在获取 opactiy 时需要获取小数 parseFloat
  return NumberMode == "float" ? parseFloat(target) : parseInt(target);
};

/**
 * 运动效果
 * @param {HTMLElement} element   运动对象，必选
 * @param {JSON}        target    属性：目标值，必选
 * @param {number}      duration  运动时间，可选
 * @param {string}      mode      运动模式，可选
 * @param {function}    callback  可选，回调函数，链式动画
 */
export const animate = (
  element,
  target,
  duration = 400,
  mode = "ease-out",
  callback
) => {
  clearInterval(element.timer);

  //判断不同参数的情况
  if (duration instanceof Function) {
    callback = duration;
    duration = 400;
  } else if (duration instanceof String) {
    mode = duration;
    duration = 400;
  }

  //判断不同参数的情况
  if (mode instanceof Function) {
    callback = mode;
    mode = "ease-out";
  }

  //获取dom样式
  const attrStyle = attr => {
    if (attr === "opacity") {
      return Math.round(getStyle(element, attr, "float") * 100);
    } else {
      return getStyle(element, attr);
    }
  };
  //根字体大小，需要从此将 rem 改成 px 进行运算
  const rootSize = parseFloat(document.documentElement.style.fontSize);

  const unit = {};
  const initState = {};

  //获取目标属性单位和初始样式值
  Object.keys(target).forEach(attr => {
    if (/[^\d^\.]+/gi.test(target[attr])) {
      unit[attr] = target[attr].match(/[^\d^\.]+/gi)[0] || "px";
    } else {
      unit[attr] = "px";
    }
    initState[attr] = attrStyle(attr);
  });

  //去掉传入的后缀单位
  Object.keys(target).forEach(attr => {
    if (unit[attr] == "rem") {
      target[attr] = Math.ceil(parseInt(target[attr]) * rootSize);
    } else {
      target[attr] = parseInt(target[attr]);
    }
  });

  let flag = true; //假设所有运动到达终点
  const remberSpeed = {}; //记录上一个速度值,在ease-in模式下需要用到
  element.timer = setInterval(() => {
    Object.keys(target).forEach(attr => {
      let iSpeed = 0; //步长
      let status = false; //是否仍需运动
      let iCurrent = attrStyle(attr) || 0; //当前元素属性址
      let speedBase = 0; //目标点需要减去的基础值，三种运动状态的值都不同
      let intervalTime; //将目标值分为多少步执行，数值越大，步长越小，运动时间越长
      switch (mode) {
        case "ease-out":
          speedBase = iCurrent;
          intervalTime = (duration * 5) / 400;
          break;
        case "linear":
          speedBase = initState[attr];
          intervalTime = (duration * 20) / 400;
          break;
        case "ease-in":
          let oldspeed = remberSpeed[attr] || 0;
          iSpeed = oldspeed + (target[attr] - initState[attr]) / duration;
          remberSpeed[attr] = iSpeed;
          break;
        default:
          speedBase = iCurrent;
          intervalTime = (duration * 5) / 400;
      }
      if (mode !== "ease-in") {
        iSpeed = (target[attr] - speedBase) / intervalTime;
        iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
      }
      //判断是否达步长之内的误差距离，如果到达说明到达目标点
      switch (mode) {
        case "ease-out":
          status = iCurrent != target[attr];
          break;
        case "linear":
          status =
            Math.abs(Math.abs(iCurrent) - Math.abs(target[attr])) >
            Math.abs(iSpeed);
          break;
        case "ease-in":
          status =
            Math.abs(Math.abs(iCurrent) - Math.abs(target[attr])) >
            Math.abs(iSpeed);
          break;
        default:
          status = iCurrent != target[attr];
      }

      if (status) {
        flag = false;
        //opacity 和 scrollTop 需要特殊处理
        if (attr === "opacity") {
          element.style.filter = "alpha(opacity:" + (iCurrent + iSpeed) + ")";
          element.style.opacity = (iCurrent + iSpeed) / 100;
        } else if (attr === "scrollTop") {
          element.scrollTop = iCurrent + iSpeed;
        } else {
          element.style[attr] = iCurrent + iSpeed + "px";
        }
      } else {
        flag = true;
      }

      if (flag) {
        clearInterval(element.timer);
        if (callback) {
          callback();
        }
      }
    });
  }, 20);
};


 /**
  * animate垂直动画滚动
  * top: 要滑动的地方
  * animateNum: 滚动动画时间（ms）
  */
 export const animateScrollY = (top, animateNum) => {
  var currentScrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  let y = (top - currentScrollTop) / animateNum * 10;  //每步的距离
  let timer = setInterval(() => {
    if (Math.abs(currentScrollTop - top) < Math.abs(y)) {
      window.scrollTo({top: top});
      clearInterval(timer);
      return;
    } else {
      window.scrollTo({top: currentScrollTop});
    }
    currentScrollTop += y;
  }, 10);
}

/**
 * 相差时间戳转成时间 DD HH:MM:SS
 */
export const differTimestampToDate = (differTimestamp) => {
  let handleTimeNum = (timeNum) => {
    return timeNum >= 10 ? timeNum : `0${timeNum}`;
  }
  let d = Math.floor(differTimestamp / (3600 * 1000 * 24));
  let leave1 = differTimestamp % (3600 * 1000 * 24);
  let h = Math.floor(leave1 / (3600 * 1000));
  let leave2 = leave1 % (3600 * 1000);  // 计算小时后剩余的毫秒数
  let m = Math.floor(leave2 / (60 * 1000));
  let leave3 = leave2 % (60 * 1000);
  let s = Math.floor(leave3 / 1000);
  return `${d > 0 ? d + '天 ' : ''}${handleTimeNum(h)}:${handleTimeNum(m)}:${handleTimeNum(s)}`;
}